﻿using FineUICore;
using Microsoft.AspNetCore.Mvc;
using SysFrame.BP;
using SysFrame.Message;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;

namespace MyAppBox
{
    public class HomeController : BaseController
    {
        public IActionResult Index()
        {
            InitTreeMenu();
            return View();
        }

        public IActionResult Hello()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult btnHello_Click()
        {
            Alert.Show("你好 FineUI！", MessageBoxIcon.Warning);

            return UIHelper.Result();
        }



        public IActionResult Login()
        {
            return View();
        }

        public ActionResult SysLogin()
        {
            SetSession("LoginUser", null);
            return View();
        }


        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult btnLogin_Click(string tbxUserName, string tbxPassword)
        {
            string username = tbxUserName;
            string psw = tbxPassword;
            if (string.IsNullOrEmpty(username))
            {
                //setMessage("请输入用户名");
                return UIHelper.Result();
            }
            if (string.IsNullOrEmpty(psw))
            {
                //setMessage("请输入密码");
                return UIHelper.Result();
            }
            BLLsystem_1 bll = new BLLsystem_1();
            MsReturned ms = new MsReturned();
            ms = bll.UserLode(username, psw);
            switch (ms.strMsId)
            {
                case "-1":
                    ShowNotify("用户名不存在！", MessageBoxIcon.Error);
                    //setMessage("用户名不存在！");
                    break;
                case "-2":
                    ShowNotify("用户名或密码错误！", MessageBoxIcon.Error);
                    //setMessage("用户名或密码错误！");
                    break;
                case "1":
                    SetSession("LoginUser", (Mssystem_1)ms.obj1);
                    ShowNotify("登录成功", MessageBoxIcon.Success);
                    return RedirectToAction("Index");
                default:
                    break;
            }

            return UIHelper.Result();
        }


        // GET: Themes
        public IActionResult Themes()
        {
            return View();
        }

        // GET: Error
        public IActionResult Error()
        {
            return View();
        }

        /// <summary>
        /// 初始化菜单
        /// </summary>
        private void InitTreeMenu()
        {
            IList<TreeNode> nodes = new List<TreeNode>();

            ViewBag.TreeMenuNodes = nodes.ToArray();

            //通过用户得到一级菜单
            BLLsystem_7 bll = new BLLsystem_7();
            List<Mssystem_7> mssys7 = new List<Mssystem_7>();
            Mssystem_1 mssys1 = GetSession<Mssystem_1>("LoginUser");
            if (mssys1 == null)
            {
                string js = "F.doPostBack({url: 'btnHomePage_Click',disableControl: 'btnHomePage'}); ";
                string conjs = FineUICore.Alert.GetShowReference("登录已过期<br/>点击【确定】跳转到登录页面", "提示", MessageBoxIcon.Warning, js, Target.Top);
                PageContext.RegisterStartupScript(conjs);
                return;
            }
            ViewBag.username = mssys1.System_1_20;
            if (mssys1.isadmin) { mssys1.sys10.System_10_20 = "超级系统管理员"; }
            ViewBag.userrole = mssys1.sys10.System_10_20;
            ViewBag.HomeTitle = new BLLsystem_4().Getsystem_4ByCode("001001").System_4_50;
            mssys7 = bll.GetHeadMenuByUser(mssys1);
            foreach (Mssystem_7 item in mssys7)
            {
                TreeNode trn = new TreeNode();
                trn.Text = item.System_7_20;
                trn.NodeID = item.System_7_10.ToString();
                trn.Leaf = false;
                trn.Expanded = false;
                BindTree(trn, item);
                nodes.Add(trn);
            }
            ViewBag.TreeMenuNodes = nodes.ToArray();
        }
        /// <summary>
        /// 重建Tree
        /// </summary>
        /// <param name="to">节点</param>
        /// <param name="item">项</param>
        private void BindTree(TreeNode to, Mssystem_7 item)
        {
            if (item != null)
            {
                List<Mssystem_7> listsys7 = new List<Mssystem_7>();
                MsQuery msq = new MsQuery();
                msq.strquery += " and SYSTEM_7_60 ='" + item.System_7_10 + "' and SYSTEM_7_40='1' ";
                msq.strorder = "SYSTEM_7_100";
                listsys7 = new BLLsystem_7().Getsystem_7MsByQuery(msq);
                if (listsys7 != null && listsys7.Count != 0)
                {
                    foreach (Mssystem_7 mslist in listsys7)
                    {
                        Mssystem_1 lodeuser = GetSession<Mssystem_1>("LoginUser");
                        if (lodeuser.menuids.Contains(mslist.System_7_10))
                        {
                            TreeNode trn = new TreeNode();
                            trn.Text = mslist.System_7_20;
                            trn.NodeID = mslist.System_7_10.ToString();
                            trn.Leaf = false;
                            if (mslist.strurl != "")//是否为子叶
                            {
                                trn.Leaf = true;
                                //拼连接字符
                                string meunid = "MenuId=" + mslist.System_7_10;
                                trn.NavigateUrl = mslist.strurl + "?" + meunid;
                                if (!string.IsNullOrEmpty(mslist.System_7_110) && mslist.System_7_110.IndexOf("&") == 0)
                                {
                                    trn.NavigateUrl += mslist.System_7_110;
                                }
                            }
                            else
                            {
                                BindTree(trn, mslist);
                            }
                            to.Nodes.Add(trn);
                        }
                    }
                }
            }
        }

    }
}